package com.example.email.service;

import com.example.common.api.EmailService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

import javax.annotation.Resource;

/**
 * @author kangshuai
 */
@Slf4j
@DubboService
public class EmailServiceImpl implements EmailService {

    @Resource
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String sendUser;


    @Override
    public boolean sendEmail(String email, String code) {
        try {
            //创建SimpleMailMessage对象
            SimpleMailMessage message = new SimpleMailMessage();
            //邮件发送人
            message.setFrom(sendUser);
            //邮件接收人
            message.setTo(email);
            //邮件主题
            message.setSubject("验证码");
            //邮件内容
            message.setText("您的验证码是: " + code + ", 1分钟内有效");
            //发送邮件
            javaMailSender.send(message);
        } catch (Exception e) {
            log.info("邮件发送异常，异常原因--{}", e.getMessage());
            return false;
        }

        return true;
    }
}
